package com.xy.mainp.locmap;

import android.databinding.DataBindingUtil;
import android.os.Bundle;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.three_lib.baidu.LocManager;
import com.xy.mainp.R;
import com.xy.mainp.base.BasePActivity;
import com.xy.mainp.databinding.ActivityLocMapBinding;
import com.xy.mainp.utils.StatusBarUtil;

/**
 * Created by John on 2016/10/27.
 */

public class LocActivity extends BasePActivity implements BDLocationListener {
    private ActivityLocMapBinding binding;
    private LocManager locManager = new LocManager();
    private boolean updateMap = true;

    @Override
    protected void initView(Bundle savedInstanceState) {
        StatusBarUtil.setImmersiveStatusBar(this,StatusBarUtil.FULL_SCREEN);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_loc_map);
        locManager.registerLocationListener(this);
        // 开启定位图层
        binding.mapView.getMap().setMyLocationEnabled(true);
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding.mapView.onDestroy();
        locManager.unRegisterLocationListener(this);
    }
    @Override
    protected void onResume() {
        super.onResume();
        binding.mapView.onResume();
        locManager.startLoc();
    }
    @Override
    protected void onPause() {
        super.onPause();
        binding.mapView.onPause();
        locManager.stopLoc();
        // 当不需要定位图层时关闭定位图层
        binding.mapView.getMap().setMyLocationEnabled(false);
    }

    @Override
    public void onReceiveLocation(BDLocation location) {

        // 构造定位数据
        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(location.getRadius())
                // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(100).latitude(location.getLatitude())
                .longitude(location.getLongitude()).build();
        // 设置定位数据
        binding.mapView.getMap().setMyLocationData(locData);

        if (updateMap){
            updateMapState(new LatLng(location.getLatitude(),location.getLongitude()));
            updateMap = false;
        }
    }

    private void updateMapState(LatLng point) {
        MapStatus ms = new MapStatus.Builder(binding.mapView.getMap().getMapStatus()).zoom(15).target(point).rotate(0).build();
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newMapStatus(ms);
        binding.mapView.getMap().animateMapStatus(mapStatusUpdate);
    }
}
